#include "Settings.h"
#include "Packet.h"
#include "DBInterface.h"

Settings * Settings::g_SettingValues = NULL;
ACE_Thread_Mutex Settings::setting_lock;

Settings* Settings::GetSettings()
{
	Settings * s = NULL;

	setting_lock.acquire();
	s = g_SettingValues;
	setting_lock.release();

	return s;
}

bool Settings::Change(Settings* new_values)
{
	if(NULL == new_values)
		return false;

	setting_lock.acquire();

	if(g_SettingValues != NULL)
		delete g_SettingValues;

	g_SettingValues = new_values;


	setting_lock.release();

	return true;
}

bool Settings::Start()
{
	Settings * new_setting = Init();

	if(NULL != new_setting && ('K' == new_setting->OPERATION_MODE || 'k' == new_setting->OPERATION_MODE) )
	{
		printf("operation mode : kill process\n");
		return false;
	}

	return Change(new_setting);
}

bool Settings::ApplyChangedSettingValue()
{
	Settings * new_settings = Settings::Init();
	if(!new_settings)
		return false;

	if( (*new_settings)  == (*Settings::g_SettingValues) )
	{
		delete new_settings;
		new_settings = NULL;
	}

	return Change(new_settings);

}

void Settings::Release()
{
	if(g_SettingValues)
		delete g_SettingValues;
}

Settings* Settings::Init()
{
	Settings * setting_values = NULL;
	Json::Value root;
	
	char buf[MAX_BUFFER];
	memset(buf , 0x00 , MAX_BUFFER);

	FILE *f = fopen( SETTING_FILE , "rb");


	if(!f)
	{
		printf("no setting file : [%s]\n" , SETTING_FILE);
		return NULL;
	}


	if(fread(buf , 1 , MAX_BUFFER , f)  > 0)
	{
		if(!PACKET_BASE::GetRoot(buf , root))
		{
			fclose(f);
			return NULL;
		}

		fclose(f);

		std::string RENDERER_ID = PACKET_BASE::GetStringS(root , "RENDERER_ID");
		if(RENDERER_ID.length() == 0)
		{
			printf("no RENDERER_ID in setting file  : [%s]\n" , SETTING_FILE);
			return NULL;
		}

		std::string DB_ADDR = PACKET_BASE::GetStringS(root , "DB_ADDR");
		if(DB_ADDR.length() == 0)
		{
			printf("no DB_ADDR in setting file  : [%s]\n" , SETTING_FILE);
			return NULL;
		}

		std::string DB_USER = PACKET_BASE::GetStringS(root , "DB_USER");
		if(DB_USER.length() == 0)
		{
			printf("no DB_USER in setting file  : [%s]\n" , SETTING_FILE);
			return NULL;
		}

		std::string DB_PWD = PACKET_BASE::GetStringS(root , "DB_PWD");
		if(DB_PWD.length() == 0)
		{
			printf("no DB_PWD in setting file  : [%s]\n" , SETTING_FILE);
			return NULL;
		}

		std::string DB_NAME = PACKET_BASE::GetStringS(root , "DB_NAME");
		if(DB_NAME.length() == 0)
		{
			printf("no DB_NAME in setting file  : [%s]\n" , SETTING_FILE);
			return NULL;
		}

		int DB_PORT = PACKET_BASE::GetIntS(root , "DB_PORT");
		if(DB_PORT == 0)
		{
			printf("no DB_PORT in setting file  : [%s]\n" , SETTING_FILE);
			return NULL;
		}

		int MAX_CONNECTION = PACKET_BASE::GetIntS(root , "MAX_CONNECTION");
		if(MAX_CONNECTION == 0)
		{
			printf("no MAX_CONNECTION in setting file  : [%s]\n" , SETTING_FILE);
			return NULL;
		}

		int MIN_CONNECTION = PACKET_BASE::GetIntS(root , "MIN_CONNECTION");
		if(MIN_CONNECTION == 0)
		{
		}

		DBInterface::Init(DB_ADDR , DB_USER , DB_PWD , DB_NAME , DB_PORT , MAX_CONNECTION , MIN_CONNECTION);
		setting_values = new Settings(RENDERER_ID);

		if(!setting_values->ReadFromDB())
		{
			delete setting_values;
			setting_values = NULL;
		}

	}
	else
	{
		printf("empty setting file  : [%s]\n" , SETTING_FILE);
		return NULL;
	}

	return setting_values;
}

std::string Settings::dump()
{
	std::string ret;
	char buf[1024];

	sprintf(buf , "Settings =>\n"); ret += buf;

	ret += std::string("RENDERER_ID : ") + RENDERER_ID + "\n";

	sprintf(buf , "SPEED : %s\n" , SPEED);  ret += buf;
	sprintf(buf , "OBJECT_DIR : %s\n" , OBJECT_DIR);  ret += buf;
	sprintf(buf , "ADD_DIR : %s\n" , ADD_DIR);  ret += buf;
	sprintf(buf , "TMP_DIR : %s\n" , TMP_DIR);  ret += buf;

	sprintf(buf , "LOG_TYPE : %c\n" , LOG_TYPE); ret += buf;
	sprintf(buf , "LOG_LEVEL : %c\n" , LOG_LEVEL); ret += buf;

	sprintf(buf , "SCALING_LOG_DIR : %s\n" , SCALING_LOG_DIR);  ret += buf;
	sprintf(buf , "PROCESS_LOG_DIR : %s\n" , PROCESS_LOG_DIR);  ret += buf;
	sprintf(buf , "JOB_LOG_DIR : %s\n" , JOB_LOG_DIR);  ret += buf;

	sprintf(buf , "HIGH_WIDTH : %d\n" , HIGH_WIDTH);  ret += buf;
	sprintf(buf , "HIGH_HEIGHT : %d\n" , HIGH_HEIGHT);  ret += buf;
	sprintf(buf , "HIGH_WIDTH2 : %d\n" , HIGH_WIDTH2);  ret += buf;
	sprintf(buf , "HIGH_HEIGHT2 : %d\n" , HIGH_HEIGHT2);  ret += buf;

	sprintf(buf , "HIGH_BITRATE : %dk\n" , HIGH_BITRATE);  ret += buf;
	sprintf(buf , "HIGH_AUDIO_BITRATE : %dk\n" , HIGH_AUDIO_BITRATE);  ret += buf;

	sprintf(buf , "MIDDLE_WIDTH : %d\n" , MIDDLE_WIDTH);  ret += buf;
	sprintf(buf , "MIDDLE_HEIGHT : %d\n" , MIDDLE_HEIGHT);  ret += buf;
	sprintf(buf , "MIDDLE_WIDTH2 : %d\n" , MIDDLE_WIDTH2);  ret += buf;
	sprintf(buf , "MIDDLE_HEIGHT2 : %d\n" , MIDDLE_HEIGHT2);  ret += buf;

	sprintf(buf , "MIDDLE_BITRATE : %dk\n" , MIDDLE_BITRATE);  ret += buf;
	sprintf(buf , "MIDDLE_AUDIO_BITRATE : %dk\n" , MIDDLE_AUDIO_BITRATE);  ret += buf;

	sprintf(buf , "LOW_WIDTH : %d\n" , LOW_WIDTH);  ret += buf;
	sprintf(buf , "LOW_HEIGHT : %d\n" , LOW_HEIGHT);  ret += buf;
	sprintf(buf , "LOW_WIDTH2 : %d\n" , LOW_WIDTH2);  ret += buf;
	sprintf(buf , "LOW_HEIGHT2 : %d\n" , LOW_HEIGHT2);  ret += buf;

	sprintf(buf , "LOW_BITRATE : %dk\n" , LOW_BITRATE);  ret += buf;
	sprintf(buf , "LOW_AUDIO_BITRATE : %dk\n" , LOW_AUDIO_BITRATE);  ret += buf;

	sprintf(buf , "PRIMARY_CONTROLLER_ADDR : %s\n" , PRIMARY_CONTROLLER_ADDR);  ret += buf;
	sprintf(buf , "SECONDARY_CONTROLLER_ADDR : %s\n" , SECONDARY_CONTROLLER_ADDR);  ret += buf;
	sprintf(buf , "CONTROLLER_PORT : %d\n" , CONTROLLER_PORT);  ret += buf;

	sprintf(buf , "CMS_VERSION : %s\n" , CMS_VERSION);  ret += buf;
	sprintf(buf , "CMS_PORT : %d\n" , CMS_PORT);  ret += buf;


	sprintf(buf , "FRAME_RATE : %d\n" , FRAME_RATE);  ret += buf;
	sprintf(buf , "IFRAME_INTERVAL : %d\n" , IFRAME_INTERVAL);  ret += buf;
	sprintf(buf , "SAMPLING_RATE : %d\n" , SAMPLING_RATE);  ret += buf;

	sprintf(buf , "MAX_IMAGE : %d\n" , MAX_IMAGE);  ret += buf;
	sprintf(buf , "MAX_EFFECT : %d\n" , MAX_EFFECT);  ret += buf;
	sprintf(buf , "EFFECT_MIN_SIZE : %.2f\n" , EFFECT_MIN_SIZE);  ret += buf;
	sprintf(buf , "EFFECT_MAX_SIZE : %.2f\n" , EFFECT_MAX_SIZE);  ret += buf;

	sprintf(buf , "CONNECTION_CHECK_DELAY : %d\n" , CONNECTION_CHECK_DELAY); ret += buf;
	sprintf(buf , "CONNECTION_CHECK_COUNT : %d\n" , CONNECTION_CHECK_COUNT); ret += buf;
	sprintf(buf , "JOB_CHECK_DELAY : %d(sec)\n" , JOB_CHECK_DELAY); ret += buf;		
	sprintf(buf , "SETTING_CHECK_COUNT : %d\n" , SETTING_CHECK_COUNT); ret += buf;		

	sprintf(buf , "MAX_AGENT : %d\n" , MAX_AGENT); ret += buf;
	sprintf(buf , "RENDERER_LOG_MODE : %s\n" , RENDERER_LOG_MODE); ret += buf;

	sprintf(buf , "RENDERER_CPU_MULTIPLE : %.2f\n" , RENDERER_CPU_MULTIPLE); ret += buf;
	sprintf(buf , "RENDERER_THREAD_COUNT : %d\n" , RENDERER_THREAD_COUNT); ret += buf;

	sprintf(buf , "OPERATION_MODE : %c\n" , OPERATION_MODE); ret += buf;

	sprintf(buf , "SCALE_UP_INTERVAL : %d(min)\n" , SCALE_UP_INTERVAL); ret += buf;
	sprintf(buf , "SCALE_DOWN_INTERVAL : %d(min)\n" , SCALE_DOWN_INTERVAL); ret += buf;
	sprintf(buf , "RENDERING_PER_MIN : %d\n" , RENDERING_PER_MIN); ret += buf;
	sprintf(buf , "AGENT_BLOCK_SIZE : %d\n" , AGENT_BLOCK_SIZE); ret += buf;
	sprintf(buf , "BLOCK_CALC_RATIO : %.2f(%)\n" , BLOCK_CALC_RATIO); ret += buf;


	sprintf(buf , "MAX_PROCESS_TIME : %d(m)\n" , MAX_PROCESS_TIME); ret += buf;

	sprintf(buf , "MAX_JOB_LIST : %d\n" , MAX_JOB_LIST); ret += buf;
	sprintf(buf , "MIN_JOB_LIST : %d\n" , MIN_JOB_LIST); ret += buf;

	sprintf(buf , "BGM_FADE_DURATION : %.2f(sec)\n" , BGM_FADE_DURATION); ret += buf;
	sprintf(buf , "SOUND_FADE_DURATION : %.2f(sec)\n" , SOUND_FADE_DURATION); ret += buf;

	sprintf(buf , "MAX_MOVIE_TIME : %d(sec)\n" , MAX_MOVIE_TIME); ret += buf;

	sprintf(buf , "S3_RETRY_COUNT : %d\n" , S3_RETRY_COUNT); ret += buf;
	sprintf(buf , "QUEUE_DELAY : %d(sec)\n" , QUEUE_DELAY); ret += buf;


	sprintf(buf , "PUSH_UUID : %s\n" , PUSH_UUID); ret += buf;
	sprintf(buf , "PUSH_SECRET_KEY : %s\n" , PUSH_SECRET_KEY); ret += buf;
	sprintf(buf , "PUSH_INFO_GROUP_CODE : %s\n" , PUSH_INFO_GROUP_CODE); ret += buf;
	sprintf(buf , "PUSH_ERROR_GROUP_CODE : %s\n" , PUSH_ERROR_GROUP_CODE); ret += buf;
	sprintf(buf , "PUSH_URL : %s\n" , PUSH_URL); ret += buf;
	sprintf(buf , "PUSH_ALIVE_TICK_DELAY : %d(min)\n" , PUSH_ALIVE_TICK_DELAY); ret += buf;

	sprintf(buf , "ADD_TRAILER : %c\n" , ADD_TRAILER); ret += buf;
	sprintf(buf , "TRAILER_FILE_NAME : %s\n" , TRAILER_FILE_NAME); ret += buf;
	sprintf(buf , "TRAILER_FILE_NAME2 : %s\n" , TRAILER_FILE_NAME2); ret += buf;
	sprintf(buf , "TRAILER_TIME : %.2f(sec)\n" , TRAILER_TIME); ret += buf;
	sprintf(buf , "TRAILER_FADE_TIME : %.2f(sec)\n" , TRAILER_FADE_TIME); ret += buf;

	sprintf(buf , "ADD_WATERMARK : %c\n" , ADD_WATERMARK); ret += buf;
	sprintf(buf , "WATERMARK_FILE_NAME : %s\n" , WATERMARK_FILE_NAME); ret += buf;
	sprintf(buf , "WATERMARK_HEIGHT_RATIO : %d(%)\n" , WATERMARK_HEIGHT_RATIO); ret += buf;
	sprintf(buf , "WATERMARK_MARGIN_RATIO : %d(%)\n" , WATERMARK_MARGIN_RATIO); ret += buf;
	sprintf(buf , "WATERMARK_HEIGHT_RATIO2 : %d(%)\n" , WATERMARK_HEIGHT_RATIO2); ret += buf;
	sprintf(buf , "WATERMARK_MARGIN_RATIO2 : %d(%)\n" , WATERMARK_MARGIN_RATIO2); ret += buf;

	sprintf(buf , "SEGMENT_TIME : %d(s)\n" , SEGMENT_TIME); ret += buf;

	return ret;

}

#define DB_SETTING_VALUE_COUNT 79
void Settings::MatchDBValues(MYSQL_BIND * pArr)
{
	DBInterface::Match2StringValue(&pArr[0] , SPEED , sizeof(SPEED) , true );
	DBInterface::Match2StringValue(&pArr[1] , OBJECT_DIR , sizeof(OBJECT_DIR) , true );
	DBInterface::Match2StringValue(&pArr[2] , ADD_DIR , sizeof(ADD_DIR) , true );
	DBInterface::Match2StringValue(&pArr[3] , TMP_DIR , sizeof(TMP_DIR) , true );

	DBInterface::Match2CharValue(&pArr[4] , &LOG_TYPE);
	DBInterface::Match2CharValue(&pArr[5] , &LOG_LEVEL);

	DBInterface::Match2StringValue(&pArr[6] , SCALING_LOG_DIR , sizeof(SCALING_LOG_DIR) , true );
	DBInterface::Match2StringValue(&pArr[7] , PROCESS_LOG_DIR , sizeof(PROCESS_LOG_DIR) , true );
	DBInterface::Match2StringValue(&pArr[8] , JOB_LOG_DIR , sizeof(JOB_LOG_DIR) , true );

	DBInterface::Match2IntValue(&pArr[9] , &HIGH_WIDTH);
	DBInterface::Match2IntValue(&pArr[10] , &HIGH_HEIGHT);
	DBInterface::Match2IntValue(&pArr[11] , &HIGH_WIDTH2);
	DBInterface::Match2IntValue(&pArr[12] , &HIGH_HEIGHT2);


	DBInterface::Match2IntValue(&pArr[13] , &HIGH_BITRATE);
	DBInterface::Match2IntValue(&pArr[14] , &HIGH_AUDIO_BITRATE);

	DBInterface::Match2IntValue(&pArr[15] , &MIDDLE_WIDTH);
	DBInterface::Match2IntValue(&pArr[16] , &MIDDLE_HEIGHT);
	DBInterface::Match2IntValue(&pArr[17] , &MIDDLE_WIDTH2);
	DBInterface::Match2IntValue(&pArr[18] , &MIDDLE_HEIGHT2);

	DBInterface::Match2IntValue(&pArr[19] , &MIDDLE_BITRATE);
	DBInterface::Match2IntValue(&pArr[20] , &MIDDLE_AUDIO_BITRATE);

	DBInterface::Match2IntValue(&pArr[21] , &LOW_WIDTH);
	DBInterface::Match2IntValue(&pArr[22] , &LOW_HEIGHT);
	DBInterface::Match2IntValue(&pArr[23] , &LOW_WIDTH2);
	DBInterface::Match2IntValue(&pArr[24] , &LOW_HEIGHT2);


	DBInterface::Match2IntValue(&pArr[25] , &LOW_BITRATE);
	DBInterface::Match2IntValue(&pArr[26] , &LOW_AUDIO_BITRATE);

	DBInterface::Match2StringValue(&pArr[27] , PRIMARY_CONTROLLER_ADDR , sizeof(PRIMARY_CONTROLLER_ADDR) , true );
	DBInterface::Match2StringValue(&pArr[28] , SECONDARY_CONTROLLER_ADDR , sizeof(SECONDARY_CONTROLLER_ADDR) , true );
	DBInterface::Match2IntValue(&pArr[29] , &CONTROLLER_PORT);

	DBInterface::Match2StringValue(&pArr[30] , CMS_VERSION , sizeof(CMS_VERSION) , true );
	DBInterface::Match2IntValue(&pArr[31] , &CMS_PORT);

	DBInterface::Match2IntValue(&pArr[32] , &FRAME_RATE);
	DBInterface::Match2IntValue(&pArr[33] , &IFRAME_INTERVAL);
	DBInterface::Match2IntValue(&pArr[34] , &SAMPLING_RATE);

	DBInterface::Match2IntValue(&pArr[35] , &MAX_IMAGE);
	DBInterface::Match2IntValue(&pArr[36] , &MAX_EFFECT);
	DBInterface::Match2DoubleValue(&pArr[37] , &EFFECT_MIN_SIZE);
	DBInterface::Match2DoubleValue(&pArr[38] , &EFFECT_MAX_SIZE);

										 
	DBInterface::Match2IntValue(&pArr[39] , &CONNECTION_CHECK_DELAY);
	DBInterface::Match2IntValue(&pArr[40] , &CONNECTION_CHECK_COUNT);
	DBInterface::Match2IntValue(&pArr[41] , &JOB_CHECK_DELAY);
	DBInterface::Match2IntValue(&pArr[42] , &SETTING_CHECK_COUNT);

	DBInterface::Match2IntValue(&pArr[43] , &MAX_AGENT);
	DBInterface::Match2StringValue(&pArr[44] , RENDERER_LOG_MODE , sizeof(RENDERER_LOG_MODE) , true );

	DBInterface::Match2DoubleValue(&pArr[45] , &RENDERER_CPU_MULTIPLE);
	DBInterface::Match2IntValue(&pArr[46] , &RENDERER_THREAD_COUNT);

	DBInterface::Match2CharValue(&pArr[47] , &OPERATION_MODE);

	DBInterface::Match2IntValue(&pArr[48] , &SCALE_UP_INTERVAL);
	DBInterface::Match2IntValue(&pArr[49] , &SCALE_DOWN_INTERVAL);
	DBInterface::Match2IntValue(&pArr[50] , &RENDERING_PER_MIN);
	DBInterface::Match2IntValue(&pArr[51] , &AGENT_BLOCK_SIZE);
	DBInterface::Match2DoubleValue(&pArr[52] , &BLOCK_CALC_RATIO);

	DBInterface::Match2IntValue(&pArr[53] , &MAX_PROCESS_TIME);

	DBInterface::Match2IntValue(&pArr[54] , &MAX_JOB_LIST);
	DBInterface::Match2IntValue(&pArr[55] , &MIN_JOB_LIST);

	DBInterface::Match2DoubleValue(&pArr[56] , &BGM_FADE_DURATION);
	DBInterface::Match2DoubleValue(&pArr[57] , &SOUND_FADE_DURATION);

	DBInterface::Match2IntValue(&pArr[58] , &MAX_MOVIE_TIME);

	DBInterface::Match2IntValue(&pArr[59] , &S3_RETRY_COUNT);
	DBInterface::Match2IntValue(&pArr[60] , &QUEUE_DELAY);

	DBInterface::Match2StringValue(&pArr[61] , PUSH_UUID , sizeof(PUSH_UUID) , true );
	DBInterface::Match2StringValue(&pArr[62] , PUSH_SECRET_KEY , sizeof(PUSH_SECRET_KEY) , true );
	DBInterface::Match2StringValue(&pArr[63] , PUSH_INFO_GROUP_CODE , sizeof(PUSH_INFO_GROUP_CODE) , true );
	DBInterface::Match2StringValue(&pArr[64] , PUSH_ERROR_GROUP_CODE , sizeof(PUSH_ERROR_GROUP_CODE) , true );
	DBInterface::Match2StringValue(&pArr[65] , PUSH_URL , sizeof(PUSH_URL) , true );
	DBInterface::Match2IntValue(&pArr[66] , &PUSH_ALIVE_TICK_DELAY);

	DBInterface::Match2CharValue(&pArr[67] , &ADD_TRAILER);
	DBInterface::Match2StringValue(&pArr[68] , TRAILER_FILE_NAME , sizeof(TRAILER_FILE_NAME) , true );
	DBInterface::Match2StringValue(&pArr[69] , TRAILER_FILE_NAME2 , sizeof(TRAILER_FILE_NAME2) , true );
	DBInterface::Match2DoubleValue(&pArr[70] , &TRAILER_TIME);
	DBInterface::Match2DoubleValue(&pArr[71] , &TRAILER_FADE_TIME);

	DBInterface::Match2CharValue(&pArr[72] , &ADD_WATERMARK);
	DBInterface::Match2StringValue(&pArr[73] , WATERMARK_FILE_NAME , sizeof(WATERMARK_FILE_NAME) , true );
	DBInterface::Match2IntValue(&pArr[74] , &WATERMARK_HEIGHT_RATIO);
	DBInterface::Match2IntValue(&pArr[75] , &WATERMARK_MARGIN_RATIO);
	DBInterface::Match2IntValue(&pArr[76] , &WATERMARK_HEIGHT_RATIO2);
	DBInterface::Match2IntValue(&pArr[77] , &WATERMARK_MARGIN_RATIO2);

	DBInterface::Match2IntValue(&pArr[78] , &SEGMENT_TIME);


}


bool Settings::ReadFromDB()
{

	MYSQL_BIND param[1] , result[DB_SETTING_VALUE_COUNT];

	std::string sql = "SELECT ";
	sql += "SPEED ,"; 
	sql += "OBJECT_DIR ,";
	sql += "ADD_DIR ,";
	sql += "TMP_DIR ,";
	sql += "LOG_TYPE ,";
	sql += "LOG_LEVEL ,";
	sql += "SCALING_LOG_DIR ,";
	sql += "PROCESS_LOG_DIR ,";
	sql += "JOB_LOG_DIR ,";

	sql += "HIGH_WIDTH ,";
	sql += "HIGH_HEIGHT ,";
	sql += "HIGH_WIDTH2 ,";
	sql += "HIGH_HEIGHT2 ,";

	sql += "HIGH_BITRATE ,";
	sql += "HIGH_AUDIO_BITRATE ,";

	sql += "MIDDLE_WIDTH ,";
	sql += "MIDDLE_HEIGHT ,";
	sql += "MIDDLE_WIDTH2 ,";
	sql += "MIDDLE_HEIGHT2 ,";

	sql += "MIDDLE_BITRATE ,";
	sql += "MIDDLE_AUDIO_BITRATE ,";

	sql += "LOW_WIDTH ,";
	sql += "LOW_HEIGHT ,";
	sql += "LOW_WIDTH2 ,";
	sql += "LOW_HEIGHT2 ,";

	sql += "LOW_BITRATE ,";
	sql += "LOW_AUDIO_BITRATE ,";

	sql += "PRIMARY_CONTROLLER_ADDR ,";
	sql += "SECONDARY_CONTROLLER_ADDR ,";
	sql += "CONTROLLER_PORT ,";

	sql += "CMS_VERSION ,";        
	sql += "CMS_PORT ,";        

	sql += "FRAME_RATE ,";
	sql += "IFRAME_INTERVAL ,";
	sql += "SAMPLING_RATE ,";

	sql += "MAX_IMAGE ,";       
	sql += "MAX_EFFECT ,";
	sql += "EFFECT_MIN_SIZE ,";
	sql += "EFFECT_MAX_SIZE ,";

	sql += "CONNECTION_CHECK_DELAY ,";
	sql += "CONNECTION_CHECK_COUNT ,";
	sql += "JOB_CHECK_DELAY ,";
	sql += "SETTING_CHECK_COUNT ,";
	sql += "MAX_AGENT ,";
	sql += "RENDERER_LOG_MODE ,";
	sql += "RENDERER_CPU_MULTIPLE ,";
	sql += "RENDERER_THREAD_COUNT ,";
	sql += "OPERATION_MODE ,";

	sql += "SCALE_UP_INTERVAL ,";
	sql += "SCALE_DOWN_INTERVAL ,";
	sql += "RENDERING_PER_MIN ,";
	sql += "AGENT_BLOCK_SIZE ,";
	sql += "BLOCK_CALC_RATIO ,";


	sql += "MAX_PROCESS_TIME ,";

	sql += "MAX_JOB_LIST ,";
	sql += "MIN_JOB_LIST ,";

	sql += "BGM_FADE_DURATION ,";
	sql += "SOUND_FADE_DURATION ,";

	sql += "MAX_MOVIE_TIME ,";

	sql += "S3_RETRY_COUNT ,";
	sql += "QUEUE_DELAY ,";

	sql += "PUSH_UUID ,";
	sql += "PUSH_SECRET_KEY ,";
	sql += "PUSH_INFO_GROUP_CODE ,";
	sql += "PUSH_ERROR_GROUP_CODE ,";
	sql += "PUSH_URL,";
	sql += "PUSH_ALIVE_TICK_DELAY ,";

	sql += "ADD_TRAILER ,";
	sql += "TRAILER_FILE_NAME ,";
	sql += "TRAILER_FILE_NAME2 ,";
	sql += "TRAILER_TIME ,";
	sql += "TRAILER_FADE_TIME ,";

	sql += "ADD_WATERMARK ,";
	sql += "WATERMARK_FILE_NAME ,";
	sql += "WATERMARK_HEIGHT_RATIO ,";
	sql += "WATERMARK_MARGIN_RATIO ,";
	sql += "WATERMARK_HEIGHT_RATIO2 ,";
	sql += "WATERMARK_MARGIN_RATIO2 ,";

	sql += "SEGMENT_TIME ";

	sql += " FROM TBL_SETTING WHERE RENDERER_ID=?";


	memset (param, 0x00, sizeof (param)); 
	memset (result, 0x00, sizeof (result)); 

	DBInterface::Match2StringValue(&param[0] , (char*)RENDERER_ID.c_str() , RENDERER_ID.length() , false );


	MatchDBValues(result);

	_DB_RESULT db_result = DBInterface::SelectOne(sql , param , result);
	if(_DB_SUCCESS == db_result)
	{

		CalcValues();
	}

	return _DB_SUCCESS == db_result;

}

void Settings::CalcValues()
{
	FRAME_INTERVAL =  (1.0 / (double)FRAME_RATE);


	std::string mode = "quiet"; 
	if( strcmp(RENDERER_LOG_MODE , "panic") == 0 || strcmp(RENDERER_LOG_MODE , "fatal") == 0 ||
		strcmp(RENDERER_LOG_MODE , "error") == 0 || strcmp(RENDERER_LOG_MODE , "warning") == 0 || strcmp(RENDERER_LOG_MODE , "info") == 0 ||
		strcmp(RENDERER_LOG_MODE , "verbose") == 0 || strcmp(RENDERER_LOG_MODE , "debug") == 0)
	{
		mode = RENDERER_LOG_MODE;
	}

	PRESET = "veryfast"; 
	if(	strcmp(SPEED , "ultrafast") == 0||strcmp(SPEED , "superfast") == 0||strcmp(SPEED , "veryfast") == 0||strcmp(SPEED , "faster") == 0||strcmp(SPEED , "fast") == 0||
		strcmp(SPEED , "medium") == 0||strcmp(SPEED , "slow") == 0||strcmp(SPEED , "slower") == 0||strcmp(SPEED , "veryslow") == 0||strcmp(SPEED , "placebo") == 0)
	{
		PRESET = SPEED;
	}

	if(HIGH_WIDTH <= 0)             HIGH_WIDTH      		=1280;
	if(HIGH_HEIGHT <= 0)            HIGH_HEIGHT     		=720;
	if(HIGH_WIDTH2 <= 0)            HIGH_WIDTH2      		=720;
	if(HIGH_HEIGHT2 <= 0)           HIGH_HEIGHT2     		=720;

	if(HIGH_BITRATE	<= 0)           HIGH_BITRATE			=2500;
	if(HIGH_AUDIO_BITRATE	<= 0)   HIGH_AUDIO_BITRATE		=128;

	if(MIDDLE_WIDTH  <= 0)          MIDDLE_WIDTH    		=854;
	if(MIDDLE_HEIGHT <= 0)          MIDDLE_HEIGHT   		=480;
	if(MIDDLE_WIDTH2  <= 0)         MIDDLE_WIDTH2    		=480;
	if(MIDDLE_HEIGHT2 <= 0)         MIDDLE_HEIGHT2   		=480;

	if(MIDDLE_BITRATE <= 0)         MIDDLE_BITRATE			=1200;
	if(MIDDLE_AUDIO_BITRATE	<= 0)   MIDDLE_AUDIO_BITRATE	=96;

	if(LOW_WIDTH <= 0)              LOW_WIDTH       		=426;
	if(LOW_HEIGHT <= 0)             LOW_HEIGHT      		=240;
	if(LOW_WIDTH2 <= 0)             LOW_WIDTH2       		=240;
	if(LOW_HEIGHT2 <= 0)            LOW_HEIGHT2      		=240;

	if(LOW_BITRATE <= 0)            LOW_BITRATE				=300;
	if(LOW_AUDIO_BITRATE	<= 0)   LOW_AUDIO_BITRATE		=64;


	if(CONTROLLER_PORT <= 0)        CONTROLLER_PORT 		=14156;
	if(CMS_PORT <= 0)				CMS_PORT     			=80;


	if(FRAME_RATE <= 0)             FRAME_RATE				=30;
	if(IFRAME_INTERVAL < 0)         IFRAME_INTERVAL			=0; 
	if(SAMPLING_RATE <= 0)          SAMPLING_RATE			=44100;

	if(MAX_IMAGE <= 0)              MAX_IMAGE       		=60;
	if(MAX_EFFECT <= 0)             MAX_EFFECT				=10;
	if(EFFECT_MIN_SIZE <= 0.0)      EFFECT_MIN_SIZE			=70.0;
	if(EFFECT_MAX_SIZE <= 0.0)      EFFECT_MAX_SIZE			=2000.0;

	if(CONNECTION_CHECK_DELAY <= 0) CONNECTION_CHECK_DELAY	=3;
	if(CONNECTION_CHECK_COUNT <= 0) CONNECTION_CHECK_COUNT	=5;
	if(JOB_CHECK_DELAY <= 0)        JOB_CHECK_DELAY			=2;
	if(SETTING_CHECK_COUNT <= 0)    SETTING_CHECK_COUNT		=10;


	if(MAX_AGENT <= 0)              MAX_AGENT				=100;

	if(SCALE_UP_INTERVAL >= SCALE_DOWN_INTERVAL)
	{
		SCALE_UP_INTERVAL		=3;
		SCALE_DOWN_INTERVAL		=55;
	}
	else
	{
		if(SCALE_UP_INTERVAL <= 0)   SCALE_UP_INTERVAL			=3;
		if(SCALE_DOWN_INTERVAL <= 0)   SCALE_DOWN_INTERVAL		=54;
	}


	if(RENDERING_PER_MIN <= 0)   RENDERING_PER_MIN	=6;
	if(AGENT_BLOCK_SIZE <= 0)   AGENT_BLOCK_SIZE	=3;
	if(BLOCK_CALC_RATIO <= 0 ) BLOCK_CALC_RATIO = 0.3;

	if(MAX_PROCESS_TIME <= 0)       MAX_PROCESS_TIME		=10;

	if(MAX_JOB_LIST <= MIN_JOB_LIST)
	{
		MAX_JOB_LIST			=500;
		MIN_JOB_LIST			=150;
	}
	else
	{
		if(MAX_JOB_LIST <=0)			MAX_JOB_LIST			=500;
		if(MIN_JOB_LIST <=0)			MIN_JOB_LIST			=150;
	}

	if(BGM_FADE_DURATION <= 0.1)		BGM_FADE_DURATION		=2.0; 
	if(SOUND_FADE_DURATION <= 0.0)	SOUND_FADE_DURATION		=0.5;

	if(MAX_MOVIE_TIME <= 0)		MAX_MOVIE_TIME				=30; 

	if(S3_RETRY_COUNT <= 0)		S3_RETRY_COUNT				=3;

	if(QUEUE_DELAY <= 0)		QUEUE_DELAY					= 1;

	if(PUSH_ALIVE_TICK_DELAY <= 0) PUSH_ALIVE_TICK_DELAY	=3;

	if('T' == ADD_TRAILER || 't' == ADD_TRAILER)
	{
		if(TRAILER_TIME <= 0.0)
			TRAILER_TIME = 2.0;

		if(TRAILER_FADE_TIME <= 0.0)
			TRAILER_FADE_TIME = 1.0;

	}

	if('T' == ADD_WATERMARK || 't' == ADD_WATERMARK)
	{
		if(WATERMARK_HEIGHT_RATIO <= 0.0)
			WATERMARK_HEIGHT_RATIO = 10;

		if(WATERMARK_MARGIN_RATIO <= 0.0)
			WATERMARK_MARGIN_RATIO = 50;

		if(WATERMARK_HEIGHT_RATIO2 <= 0.0)
			WATERMARK_HEIGHT_RATIO2 = 8;

		if(WATERMARK_MARGIN_RATIO2 <= 0.0)
			WATERMARK_MARGIN_RATIO2 = 40;

	}

	if(SEGMENT_TIME <=0) SEGMENT_TIME=5;


	if( 0 > RENDERER_THREAD_COUNT || (int)ACE_OS::num_processors() < RENDERER_THREAD_COUNT)
		RENDERER_THREAD_COUNT = 1; 
	if( 0 >= RENDERER_CPU_MULTIPLE || MAX_CPU_MULTIPLE < RENDERER_CPU_MULTIPLE)
		RENDERER_CPU_MULTIPLE = 1; 

	memset(RENDERER_HEAD_CMD , 0x00 , sizeof(RENDERER_HEAD_CMD));
	sprintf(RENDERER_HEAD_CMD , "ffmpeg -loglevel %s -threads %d -y " , mode.c_str() , RENDERER_THREAD_COUNT);




#ifdef ACE_WIN32
	char dir_divider = '\\';
#else
	char dir_divider = '/';
#endif

	sprintf(OBJECT_DIR	, "%s%c" , OBJECT_DIR		,dir_divider);
	sprintf(ADD_DIR		, "%s%c" , ADD_DIR		,dir_divider);
	sprintf(TMP_DIR		, "%s%c" , TMP_DIR		,dir_divider);
	sprintf(SCALING_LOG_DIR	, "%s%c" , SCALING_LOG_DIR	,dir_divider);
	sprintf(PROCESS_LOG_DIR	, "%s%c" , PROCESS_LOG_DIR	,dir_divider);
	sprintf(JOB_LOG_DIR	, "%s%c" , JOB_LOG_DIR		,dir_divider);

#ifndef CONTROLLER

	CONTROLLER_ADDR = GetControllerAddr();

#endif

	GetLocalAddr();


}

std::string Settings::GetRendererTailCmd(int bv , int ba)
{
	char cmd[2014];
	memset(cmd , 0x00 , sizeof(cmd));

	if(IFRAME_INTERVAL > 0)
	{
		sprintf(cmd , " -vcodec libx264 -vprofile baseline -movflags +faststart -force_key_frames 00:00:00.000 -preset %s -b:v %dk -shortest -r %d -g %d -acodec aac -ar %d -b:a %dk -ac 2 "
			, PRESET.c_str()
			, bv 
			, FRAME_RATE 
			, IFRAME_INTERVAL
			, SAMPLING_RATE
			, ba );
	}
	else
	{
		sprintf(cmd , " -vcodec libx264 -vprofile baseline -movflags +faststart -force_key_frames 00:00:00.000 -preset %s -b:v %dk -shortest -r %d  -acodec aac -ar %d -b:a %dk -ac 2 "
			, PRESET.c_str()
			, bv
			, FRAME_RATE 
			, SAMPLING_RATE
			, ba );
	}

	return cmd;


}

std::string Settings::GetControllerAddr()
{
	std::string addr = "";

	int p_t , s_t;
	_DB_RESULT db_result = DBInterface::GetControllerTimeElapsed(RENDERER_ID , p_t , s_t);
	if(_DB_SUCCESS == db_result)
	{
		if(p_t <= s_t)
		{
			addr = PRIMARY_CONTROLLER_ADDR;
		}
		else
		{
			addr = SECONDARY_CONTROLLER_ADDR;
		}
	}

	return addr;
}


void Settings::GetLocalAddr()
{
	LOCAL_ADDR = "";

	ACE_SOCK_Stream stream;
	ACE_SOCK_Connector  connector;

	ACE_Time_Value timeout(10); 

#ifndef CONTROLLER 

	ACE_INET_Addr addr(CMS_PORT , CONTROLLER_ADDR.c_str());
	if(connector.connect(stream , addr , &timeout) != -1)
	{
		ACE_INET_Addr local_iaddr;
		if( 0 == stream.get_local_addr(local_iaddr))
		{
			char buf[100];
			local_iaddr.addr_to_string(buf , sizeof(buf));

			LOCAL_ADDR = buf;
			int find_pos = LOCAL_ADDR.find(":");
			if(find_pos  > 0)
			{
				LOCAL_ADDR = LOCAL_ADDR.substr(0 , find_pos);
			}
		}

		stream.close();
	}

#else

	ACE_INET_Addr addr(CMS_PORT , PRIMARY_CONTROLLER_ADDR);
	if(connector.connect(stream , addr , &timeout) != -1)
	{
		ACE_INET_Addr local_iaddr;
		if( 0 == stream.get_local_addr(local_iaddr))
		{
			char buf[100];
			local_iaddr.addr_to_string(buf , sizeof(buf));

			LOCAL_ADDR = buf;
			int find_pos = LOCAL_ADDR.find(":");
			if(find_pos  > 0)
			{
				LOCAL_ADDR = LOCAL_ADDR.substr(0 , find_pos);
				CONTROLLER_ADDR = LOCAL_ADDR;
			}
		}

		stream.close();
	}
	else
	{
		ACE_INET_Addr addr2(CMS_PORT , SECONDARY_CONTROLLER_ADDR);
		if(connector.connect(stream , addr2 , &timeout) != -1)
		{
			ACE_INET_Addr local_iaddr2;
			if( 0 == stream.get_local_addr(local_iaddr2))
			{
				char buf[100];
				local_iaddr2.addr_to_string(buf , sizeof(buf));

				LOCAL_ADDR = buf;
				int find_pos = LOCAL_ADDR.find(":");
				if(find_pos  > 0)
				{
					LOCAL_ADDR = LOCAL_ADDR.substr(0 , find_pos);
					CONTROLLER_ADDR = LOCAL_ADDR;
				}
			}

			stream.close();
		}
	}

#endif
	
}

#ifdef CONTROLLER 
bool Settings::IsPrimary()
{
	return LOCAL_ADDR == PRIMARY_CONTROLLER_ADDR;
}
bool Settings::IsControllerAddr()
{
	return LOCAL_ADDR == PRIMARY_CONTROLLER_ADDR || LOCAL_ADDR == SECONDARY_CONTROLLER_ADDR;
}

#else

bool Settings::IsControllerAddr()
{
	return LOCAL_ADDR == GetControllerAddr();
}

#endif



bool Settings::IsDebugMode()
{
	return 'D' == GetSettings()->LOG_LEVEL || 'd' == GetSettings()->LOG_LEVEL;
}